//
//  PlanPayView.m
//  WanCaiC8
//
//  Created by tangang on 2018/5/2.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "PlanPayView.h"
#import "PlanVM.h"
@interface PlanPayView()
@property (nonatomic ,strong) NSDictionary *dict;
@end

@implementation PlanPayView{
    NSInteger   select;
    UIButton *btnCoin;
    UIButton *btnCard;
}

- (instancetype)init{
    if (self=[super init]) {
        self.backgroundColor=[UIColor colorWithWhite:0 alpha:0.2];
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeSelf)];
        [self addGestureRecognizer:tap];
        select=1;
//        [self payView];
    }
    return self;
}

- (void)setLType:(NSInteger)lType{
    _lType=lType;
    WS(weakSelf);
    [[PlanVM manage]getReadCoinWithUid:self.userId andLType:lType Success:^(id returnValue) {
        weakSelf.dict=returnValue;
        if ([[returnValue objectForKey:@"ReadCoin"] integerValue]==0) {
            if (self.selectBlock) {
                self.selectBlock(1);
            }
        }else{
            [weakSelf payView];
        }
        
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
}

- (void)payView{
    UIView *view=[[UIView alloc]init];
    view.backgroundColor=[UIColor whiteColor];
    view.layer.masksToBounds=YES;
    view.layer.cornerRadius=5;
    [self addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.left.equalTo(self).with.offset(57);
        make.right.equalTo(self).with.offset(-57);
    }];
    
    UIButton *deleteBtn=[[UIButton alloc]init];
    [deleteBtn addTarget:self action:@selector(removeSelf) forControlEvents:UIControlEventTouchUpInside];
    [deleteBtn setImage:[UIImage imageNamed:@"search_delete"] forState:UIControlStateNormal];
    [view addSubview:deleteBtn];
    [deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(view).with.offset(10);
        make.size.mas_equalTo(CGSizeMake(28, 28));
    }];
    
    QXLable *labTitle=[[QXLable alloc]initWithBFount:18];
    labTitle.text=@"支付";
    [view addSubview:labTitle];
    [labTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view);
        make.centerY.equalTo(deleteBtn);
    }];
    
    UIView *line=[[UIView alloc]init];
    line.backgroundColor=DEFAULTBGCOLOR;
    [view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(view);
        make.top.equalTo(labTitle.mas_bottom).with.offset(15);
        make.height.mas_equalTo(@.7);
    }];
    
    QXLable *labTitle1=[[QXLable alloc]initTitle];
    labTitle1.text=@"支付金币或使用查看券查看号码";
    [view addSubview:labTitle1];
    [labTitle1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view);
        make.top.equalTo(line).with.offset(11);
    }];
    
    UIImageView *imageV=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"card_gold"]];
    [view addSubview:imageV];
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view).with.offset(23);
        make.top.equalTo(labTitle1.mas_bottom).with.offset(16);
        make.size.mas_equalTo(CGSizeMake(37, 37));
    }];
    
    NSMutableAttributedString *attStr=[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"X %@   (可用金币%@个)",[self.dict objectForKey:@"ReadCoin"],[self.dict objectForKey:@"UserCoin"]]];
    [attStr addAttribute:NSForegroundColorAttributeName value:DEFAULTTEXTGRAYCOLOR range:NSMakeRange(5, attStr.length-5)];
    QXLable  *labCoin=[[QXLable alloc]init];
    labCoin.attributedText=attStr;
    [view addSubview:labCoin];
    [labCoin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageV.mas_right).with.offset(10);
        make.centerY.equalTo(imageV);
    }];
    
    btnCoin=[[UIButton alloc]init];
    [btnCoin setImage:[UIImage imageNamed:@"card_noSelected"] forState:UIControlStateNormal];
    [btnCoin setImage:[UIImage imageNamed:@"card_selected"] forState:UIControlStateSelected];
    [btnCoin addTarget:self action:@selector(onclickCoin) forControlEvents:UIControlEventTouchUpInside];
    btnCoin.selected=YES;
    [view addSubview:btnCoin];
    [btnCoin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(labCoin);
        make.right.equalTo(view);
        make.size.mas_equalTo(CGSizeMake(56, 30));
    }];
    
    UIView *line1=[[UIView alloc]init];
    line1.backgroundColor=DEFAULTBGCOLOR;
    [view addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageV.mas_bottom).with.offset(10);
        make.left.equalTo(view).with.offset(14);
        make.right.equalTo(view).with.offset(-17);
        make.height.mas_equalTo(@.7);
    }];
    /////////
    if ([[self.dict objectForKey:@"UserCouponNum"] integerValue]>0) {
        UIImageView *imageCard=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"pay_card"]];
        [view addSubview:imageCard];
        [imageCard mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view).with.offset(23);
            make.top.equalTo(line1.mas_bottom).with.offset(10);
            make.size.mas_equalTo(CGSizeMake(37, 37));
        }];
        
        QXLable  *labCard=[[QXLable alloc]init];
        NSMutableAttributedString *attStr1=[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"X 1   (可用查看券%@张)",[self.dict objectForKey:@"UserCouponNum"]]];
        [attStr1 addAttribute:NSForegroundColorAttributeName value:DEFAULTTEXTGRAYCOLOR range:NSMakeRange(5, attStr1.length-5)];
        labCard.attributedText=attStr1;
        [view addSubview:labCard];
        [labCard mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(imageCard.mas_right).with.offset(10);
            make.centerY.equalTo(imageCard);
        }];
        
        btnCard=[[UIButton alloc]init];
        [btnCard setImage:[UIImage imageNamed:@"card_noSelected"] forState:UIControlStateNormal];
        [btnCard setImage:[UIImage imageNamed:@"card_selected"] forState:UIControlStateSelected];
        [btnCard addTarget:self action:@selector(onclickCard) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btnCard];
        [btnCard mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(labCard);
            make.right.equalTo(view);
            make.size.mas_equalTo(CGSizeMake(56, 30));
        }];
        
        UIView *line2=[[UIView alloc]init];
        line2.backgroundColor=DEFAULTBGCOLOR;
        [view addSubview:line2];
        [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(imageCard.mas_bottom).with.offset(10);
            make.left.equalTo(view).with.offset(14);
            make.right.equalTo(view).with.offset(-17);
            make.height.mas_equalTo(@.7);
        }];
        
        UIButton *payBtn=[[UIButton alloc]init];
        [payBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
        payBtn.backgroundColor=NAVCOLOR;
        
        [payBtn setTitle:@"确认支付" forState:UIControlStateNormal];
        [payBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        payBtn.layer.masksToBounds=YES;
        payBtn.layer.cornerRadius=5;
        [payBtn addTarget:self action:@selector(onclickPay) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:payBtn];
        [payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(line1);
            make.bottom.equalTo(view).with.offset(-23);
            make.top.equalTo(line2.mas_bottom).with.offset(23);
            make.height.mas_equalTo(@44);
        }];
    }else{
        UIButton *payBtn=[[UIButton alloc]init];
        [payBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
        payBtn.backgroundColor=NAVCOLOR;
        [payBtn setTitle:@"确认支付" forState:UIControlStateNormal];
        [payBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        payBtn.layer.masksToBounds=YES;
        payBtn.layer.cornerRadius=5;
        [payBtn addTarget:self action:@selector(onclickPay) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:payBtn];
        [payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(line1);
            make.bottom.equalTo(view).with.offset(-23);
            make.top.equalTo(line1.mas_bottom).with.offset(23);
            make.height.mas_equalTo(@44);
        }];
    }
    
    
    
    
}

- (void)onclickCoin{
    select=1;
    btnCoin.selected=YES;
    btnCard.selected=NO;
}

- (void)onclickCard{
    select=2;
    btnCoin.selected=NO;
    btnCard.selected=YES;
}

- (void)onclickPay{
    [self removeSelf];
    if (select==1) {
        if ([self.dict objectForKey:@"ReadCoin"]>[self.dict objectForKey:@"UserCoin"]) {
            if (self.lackCOINS) {
                self.lackCOINS();
            }
        }else{
            if (self.selectBlock) {
                self.selectBlock(select);
            }
        }
    }else{
        if (self.selectBlock) {
            self.selectBlock(select);
        }
    }
    
    
    
}

- (void)removeSelf{
    [self removeFromSuperview];
}



@end
